<!--
 * @Description: 商家优惠券列表
 * @Author: liyujie
 * @Date: 2021/02/03
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/02/03
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    商家优惠券列表
                </div>
            </div>
            <!-- /header -->
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">优惠券信息</p>
                    <el-input
                        placeholder="请输入优惠券名称或优惠券编号"
                        v-model="filterData.title"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">优惠券状态</p>
                    <el-select
                        v-model="filterData.coupon_status"
                        clearable
                        filterable
                        placeholder="全部"
                        size="medium"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in activeStatusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"/>
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">商家名称</p>
                    <el-input
                        placeholder="请输入商家名称"
                        v-model="filterData.hire_keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">创建时间</p>
                    <el-date-picker
                        v-model="createTimeRange"
                        type="datetimerange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="medium"
                        style="width: 400px"
                        value-format="timestamp"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                </div>
            </div>
            <!--筛选-->

            <!-- table -->
            <el-table
                :data="tableData"
                height="100%"
                style="width: 100%;"
                ref="tempLateName"
                class="data-table"
            >
                <el-table-column
                    label="序号"
                    align="center"
                    width="80"
                    type="index"
                />
                <el-table-column
                    prop="shop_title"
                    label="商家名称"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                    width="140"
                />
                <el-table-column
                    prop="coupon_no"
                    label="优惠券编号"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="title"
                    label="优惠券名称"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                    width="140"
                />
                <el-table-column
                    label="适用商品"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.use_limit_type === 1">全部商品可用</div>
                        <div v-if="scope.row.use_limit_type === 2">指定商品可用</div>
                        <div v-if="scope.row.use_limit_type === 4">指定商品不可用</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="discount_info"
                    label="优惠内容"
                    show-overflow-tooltip
                    align="left"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="使用时间"
                    show-overflow-tooltip
                    align="left"
                    width="200"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.use_time_type === 1">
                            <div>自：{{ scope.row.use_start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}</div>
                            <div>至：{{ scope.row.use_end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}</div>
                        </div>
                        <div v-else>领取后{{ scope.row.use_day_duration }}天内生效</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="活动状态"
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div class="status-name">{{ scope.row.coupon_status | activeStatus }}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="创建时间"
                    align="center"
                    width="200"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="发放总量"
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">{{ scope.row.total_num_max }}张</template>
                </el-table-column>
                <el-table-column
                    label="已领取"
                    align="center"
                >
                    <template slot-scope="scope">{{ scope.row.get_count }}张</template>
                </el-table-column>
                <el-table-column
                    label="剩余"
                    align="center"
                >
                    <template slot-scope="scope">{{ scope.row.total_num_max - scope.row.get_count }}张</template>
                </el-table-column>
                <el-table-column
                    label="已使用"
                    align="center"
                >
                    <template slot-scope="scope">{{ scope.row.use_count }}张</template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="left"
                    width="240"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            size="medium"
                            @click="toCouponDetail(scope.row.coupon_no, scope.row.coupon_status)"
                        >
                            查看
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="toStatisticsPage(scope.row.coupon_no)"
                            v-if="scope.row.coupon_status !== 1"
                        >
                            统计
                        </el-button>
                        <el-button
                            type="text"
                            size="medium"
                            @click="getCouponGoods(scope.row)"
                        >
                            查看可用商品
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- /table -->

            <!-- 分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>
        <!-- 选择商品 -->
        <goods-select
            ref="goodsSelect"
            title="查看可用商品"
            @change="getCouponChangeShop"
        />
        <!-- 设置商品组 -->
        <shop-group
            ref="shopGroup"
            @sendData="joinCate"
        />
    </div>
</template>

<script>
import { back } from '@/mixin/back';
import GoodsSelect from '@/components/goods/goods-select';
import shopGroup from '@/components/student/shop-group';

export default {
    name: 'ShopCouponList',
    components: {GoodsSelect,shopGroup},
    mixins: [back],
    filters: {
        activeStatus(val) {
            switch (val) {
                case 2:
                    return '未开始';
                case 3:
                    return '进行中';
                case 4:
                    return '已失效';
                case 5:
                    return '已结束';
                default:
                    return '--';
            }
        },
    },
    computed: {},
    data() {
        return {
            filterData: {
                // 优惠券名称或优惠券编号
                title: '',
                // 活动状态
                coupon_status: '',
                // 商家名称
                hire_keyword: '',
                // 创建时间
                start_time: '',
                end_time: '',
            },
            createTimeRange: [],
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            //活动状态
            activeStatusList: [
                { label: '未开始', value: 2 },
                { label: '进行中', value: 3 },
                { label: '已失效', value: 4 },
                { label: '已结束', value: 5 }
            ],
            authResult: {},
            // 推荐到商品组的商品编号
            goods_nos: ''
        };
    },
    watch: {
        createTimeRange(value) {
            this.filterData.start_time = value ? value[0] / 1000 : '';
            this.filterData.end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        },
    },

    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '商家优惠券'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取表优惠券列表数据
         * @param page
         */
        getTableData(page) {

            let params = {...this.filterData};

            params.page_id = page || this.page.now_page;
            this.$post('/student/shop_goods_coupon%5Cget_shop_coupon_list', params, resp => {
                if (resp.code === 1) {
                    this.tableData = resp.data.list;
                    this.page = resp.data.page;
                }
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 跳转优惠券详情
         * @param no - 优惠券编号
         * @param coupon_status - 活动状态
         * @param verify_status - 审核状态
         */
        toCouponDetail(no, coupon_status = '',verify_status = '') {
            this.$router.push({
                path: '/student/coupon-detail',
                query: {
                    no,
                    coupon_status,
                    verify_status,
                    mode: 'detail',
                    is_shop: 1
                }
            });
        },

        /**
         * 跳转统计页面
         * @param no
         */
        toStatisticsPage(no) {
            this.$router.push({
                path: '/student/coupon-data-statistics',
                query: {
                    no
                }
            });
        },

        /**
         * 获取优惠券商品
         */
        getCouponGoods(row) {
            let params = {
                coupon_no: row.coupon_no,
                page_size: 0
            };

            this.$post('/student/shop_goods_coupon%5Cget_coupon_goods_list', params, resp => {
                if (resp.code === 1) {
                    console.log(resp.data.list);
                    this.$refs.goodsSelect.formData.shop_no = row.shop_no;
                    this.$refs.goodsSelect.edit(3, resp.data.list);
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 获取选择的商品
         */
        getCouponChangeShop(data) {
            let goods_nos = data.map(item => {
                return item.goods_no;
            });

            this.goods_nos = goods_nos.join();
            this.$refs.shopGroup.show();
            this.$refs.shopGroup.getType('add');
        },

        /**
         * 获取推荐到商品组数据
         */
        joinCate (resp) {
            let goods_nos = this.goods_nos;

            this.$post('/student/shop_goods%5Cjoin_cate', {
                goods_nos,
                data_nos: resp.data.join()
            }, resp => {
                if (resp.code === 1) {
                    this.$refs.shopGroup.hide();
                    this.getTableData();
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },
    },
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        /*筛选*/

        .filter-view {
            margin-top: 12px;
            padding: 0 24px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 16px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
            }
        }

        /*table*/

        .data-table {
            ::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }
            .el-button {
                margin-left: 0;
                margin-right: 10px;
                .el-icon-arrow-down {
                    margin-left: 0;
                }
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding: 10px 32px;

            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
        /deep/ .el-dialog__header {
            padding-top: 10px;
            box-shadow: 0 1px 0 0 rgba(233,233,233,1);
            display: flex;
            align-items: center;
            background: #FAFAFA;
            border-radius: 4px 4px 0 0;
            .el-dialog__title {
                font-weight: 500;
                color: #333;
                font-size: 14px;
            }
            .el-icon-close:before {
                color: rgb(194,194,194);
                font-size: 17px;
                font-weight: bold;
            }
            .el-dialog__headerbtn {
                top: 14px;
            }
        }
        .el-textarea__inner {
            resize: none;
        }
    }
}
</style>
